#include<vector>
#include<deque>
using namespace std;
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>>res;
        if (root == NULL)
            return res;
        deque<Node*>queue;
        queue.push_back(root);
        int len = 1;
        int next_len = 0;
        while (!queue.empty()) {
            vector<int>tmp;
            while (len>0) {
                Node* node = queue.front();
                queue.pop_front();
                if (node == NULL) continue;
                tmp.push_back(node->val);
                for (auto n : node->children) 
                    queue.push_back(n);
                next_len += node->children.size();
                --len;
            }
            res.push_back(tmp);
            swap(len, next_len);
        }
        return res;
    }
};
